import random
import cv2


def CatVideo():
	#cv2.namedWindow("Face recognition windows form ")
	#1调用摄像头
	cap=cv2.VideoCapture(0)
	#2人脸识别器分类器
	classfier=cv2.CascadeClassifier("Train.xml")
	color=(0,255,0)
	frameNum = 1
	while cap.isOpened():
		ok,frame=cap.read()
		if not ok:
			break
		#2灰度转换
		grey=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
		#人脸检测，1.2和2分别为图片缩放比例和需要检测的有效点数
		faceRects = classfier.detectMultiScale(grey, scaleFactor = 1.2, minNeighbors = 3, minSize = (32, 32))
		if len(faceRects) > 0:     #大于0则检测到人脸
			print("The first :人脸识别")
			for faceRect in faceRects:  #单独框出每一张人脸
				x, y, w, h = faceRect
				cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 3)
		cv2.imshow("Face recognition",frame)
		print("The second : 按p拍照")
		# 按p拍照  即可实现信息采集，后期将采集的文件连接数据库以后存放即可
		if cv2.waitKey(1) & 0xFF == ord('p'):
			randomname = str(random.randint(1, 100))
			cv2.imwrite("/images/" + randomname + ".jpeg", frame)
			break
			#p
	cap.release()
	cv2.destroyAllWindows()
CatVideo() 